package DataStrucures;
// 单链表的增删改查
public class SingleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode heroNode1=new HeroNode(1,"宋江","及时雨");
        HeroNode heroNode2=new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode heroNode3=new HeroNode(3,"吴用","智多星");
        HeroNode heroNode4=new HeroNode(4,"林冲","豹子头");
        HeroNode heroNode5=new HeroNode(3,"李逵","黑旋风");
        SingleLinkedList singleLinkedList=new SingleLinkedList();
        singleLinkedList.add2(heroNode1);
        singleLinkedList.add2(heroNode4);
        singleLinkedList.add2(heroNode2);
        singleLinkedList.add2(heroNode3);
        singleLinkedList.list();
        singleLinkedList.update(heroNode5);
        singleLinkedList.delete(1);
        singleLinkedList.delete(2);
        singleLinkedList.delete(3);
        singleLinkedList.delete(4);
        singleLinkedList.list();
    }
}

class SingleLinkedList{
    private HeroNode hero=new HeroNode(0,"","");
    //创建头节点
    public void add(HeroNode heroNode){//添加新结点
        HeroNode temp=hero;
        while(true){
            if(temp.next==null){
                break;
            }
            temp=temp.next;
        }
        temp.next=heroNode;
    }
    public void list(){
        if(hero.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp=hero.next;
        while (true){
            if(temp==null){
                break;
            }
            System.out.println(temp);
            temp=temp.next;
        }
    }
    public void add2(HeroNode heroNode){//添加新结点
        HeroNode temp=hero;
        boolean flag=false;

        while(true){
            if(temp.next==null||temp.next.no> heroNode.no){
                break;
            }
            if(temp.next.no== heroNode.no){
                flag=true;
            }

            temp=temp.next;
        }
        if(flag){
            System.out.println("编号已存在");
            return;
        }
        heroNode.next=temp.next;
        temp.next=heroNode;
    }
    public void update(HeroNode newHerNode){//修改
        if(hero.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp=hero.next;
        while (true){
            if(temp.no== newHerNode.no){
                break;
            }
            if(temp.next==null){
                System.out.println("无此结点");
                return;
            }
            temp=temp.next;

        }
        temp.name= newHerNode.name;
        temp.nickname= newHerNode.nickname;


    }
    public void delete(int no){//删除
        if(hero.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp=hero;
        while(true){
            if(temp.next==null||temp.next.no==no){
                break;
            }
            temp=temp.next;
        }temp.next=temp.next.next;

    }

}



class HeroNode{
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}